import 'package:built_collection/built_collection.dart';
import 'package:equatable/equatable.dart';
import 'package:learning_content_admin_pannel.core/core/domain/entities/app_navigator_transition_info.dart';
import 'package:learning_content_admin_pannel.core/core/domain/entities/page_models/app_navigator_page_model.dart';

part 'result.dart';

abstract class HandleRootNavigatorPagePushRequest {
  RootNavigatorPagePushRequestHandlingResult call({
    required AppNavigatorPageModel pageModel,
    required BuiltList<AppNavigatorPageModel> rootNavigatorPageModels,
    required BuiltMap<AppNavigatorPageModel, AppNavigatorTransitionInfo>
        rootNavigatorPageModelToTransitionInfo,
    AppNavigatorTransitionInfo? transitionInfo,
  });
}

class HandleRootNavigatorPagePushRequestImpl implements HandleRootNavigatorPagePushRequest {
  const HandleRootNavigatorPagePushRequestImpl();

  @override
  RootNavigatorPagePushRequestHandlingResult call({
    required AppNavigatorPageModel pageModel,
    required BuiltList<AppNavigatorPageModel> rootNavigatorPageModels,
    required BuiltMap<AppNavigatorPageModel, AppNavigatorTransitionInfo>
        rootNavigatorPageModelToTransitionInfo,
    AppNavigatorTransitionInfo? transitionInfo,
  }) {
    final updatedRootNavigatorPageModels = rootNavigatorPageModels.rebuild((pageModels) {
      pageModels.add(pageModel);
    });

    const defaultTransitionInfo = AdditionNavigatorTransitionInfo(
      hasAnimation: false,
    );

    final updatedRootNavigatorPageModelToTransitionInfo =
        rootNavigatorPageModelToTransitionInfo.rebuild((pageModelToTransitionInfo) {
      pageModelToTransitionInfo[pageModel] = transitionInfo ?? defaultTransitionInfo;
    });

    final result = RootNavigatorPagePushRequestHandlingResult(
      rootNavigatorPageModels: updatedRootNavigatorPageModels,
      rootNavigatorPageModelToTransitionInfo: updatedRootNavigatorPageModelToTransitionInfo,
    );

    return result;
  }
}
